﻿/*-----------------------------------------------------------------------
 * <copyright file="MatterSelContainer.xaml.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Zhang jun</author>
 * <summary> 
 *          MatterSelSearchCtrl
 *          工程別 言語別 生産性 共用查询
 *          
 *          作成日：08/10/2010
 *                  10/15/2010 增加 日志中记录用户查询条件。
 * </summary>
 *-----------------------------------------------------------------------
 */
namespace Orid.Management.Statistics.OperatorUnitMangement.Common.SearchCtrl
{
    using System;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.MatterQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    /// 工程別 言語別 生産性 共用查询
    /// </summary>
    public partial class MatterSelContainer : UserControl
    {
        /// <summary>
        /// 当前查询框的名字（日志用）
        /// </summary>
        private string ctrlName = " 工程別/言語別 生産性共用查询框MatterSelContainer ";

        /// <summary>
        /// 查询子窗体 lineSelectWin
        /// </summary>
        childWindow.LineSelChildWin lineSelect = null;

        /// <summary>
        /// 消息子窗体 errorMessbox
        /// </summary>
        private childWindow.MessageBox errorMessbox = null;

        /// <summary>
        /// 日志服务定义
        /// wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient = null;

        /// <summary>
        /// 工程別 言語別 生産性 查询
        /// </summary>
        private WCFMatterCorrelationClient wcfMatterCorrelationClient = null;

        /// <summary>
        /// 开始日期，默认为当前日期。用于计算DataGrid表头
        /// </summary>
        public DateTime startDataTime = DateTime.Now;

        /// <summary>
        /// 查询命令 导出命令
        /// </summary>
        private Command.ICommand queryCcommand = null, exportComand = null;

        /// <summary>
        /// 记录用户查询条件
        /// </summary>
        /// <value>10/15/2010 Add</value>
        public string WhereIntorLogs
        {
            get;
            set;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public MatterSelContainer()
        {
            InitializeComponent();
            // Grid引用主题
            ((App)Application.Current).SetTheme(LayoutRoot);

            //Accordion 时的Border 控件 引用主题
            ((App)Application.Current).SetTheme(bAccordionItemBorder);

            this.InitField();

            //查询按钮
            this.btnSelList.Click += new RoutedEventHandler(btnSelList_Click);

            //数据导出
            this.btnExport.Click += new RoutedEventHandler(this.BtnExport_Click);

            //... 按钮
            this.btnSelContext.Click += new RoutedEventHandler(btnSelContext_Click);

            //线路子窗体关闭
            this.lineSelect.Closed += new EventHandler(lineSelect_Closed);

            // 开始日期(DatePicker)
            this.dpDateFrom.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(this.DpDateFrom_SelectedDateChanged);

            // 结束日期(DatePicker)
            this.dpDateTo.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(this.DpDateTo_SelectedDateChanged);

            //设置 查询界面 的 查询按钮 数据导出 初始化可用
            this.btnSelList.IsEnabled = true;
            this.btnExport.IsEnabled = true;

            // 如果不是 SV或QC，显示导出按钮
            if (!(FunctionClass.GetUserIdentity() == FunctionClass.UserIdentity.SV.ToString() ||
                    FunctionClass.GetUserIdentity() == FunctionClass.UserIdentity.QC.ToString()))
            {
                this.btnExport.Visibility = Visibility.Visible;
            }
            else
            {
                this.btnExport.Visibility = Visibility.Collapsed;
            }

            this.dpDateTo.SelectedDate = DateTime.Now.Date;
        }

        /// <summary>
        /// Gets or sets vlaue of exportComand
        /// 导出命令 ExportComand
        /// </summary>
        public Command.ICommand ExportComand
        {
            get { return this.exportComand; }
            set { this.exportComand = value; }
        }

        /// <summary>
        /// Gets or sets value of queryCcommand
        /// 查询命令 QueryCcommand
        /// </summary>
        public Command.ICommand QueryCcommand
        {
            get { return this.queryCcommand; }
            set { this.queryCcommand = value; }
        }        

        /// <summary>
        /// 变量初始化方法  InitField
        /// </summary>
        private void InitField()
        {
            this.lineSelect = new childWindow.LineSelChildWin();
            this.errorMessbox = new childWindow.MessageBox();
            this.wcfLogClient = Wcf.Service.Log4;
            this.wcfMatterCorrelationClient = Wcf.Service.MatterQuery;
        }

        /// <summary>
        /// 导出按钮 点击事件 BtnExport_Click
        /// </summary>
        /// <param name="sender">Sender of object</param>
        /// <param name="e">e type of RoutedEventArgs</param>
        private void BtnExport_Click(object sender, RoutedEventArgs e)
        {
            writeWhereLog();
            this.btnExport.IsEnabled = false;

            this.exportComand.Exec();
        }

        /// <summary>
        /// 查 询 按钮
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        void btnSelList_Click(object sender, RoutedEventArgs e)
        {
            writeWhereLog();
            this.btnSelList.IsEnabled = false;

            this.queryCcommand.Exec();
        }

        #region 子窗体相关
        /// <summary>
        /// LINE线路子窗体关闭
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        void lineSelect_Closed(object sender, EventArgs e)
        {
            try
            {
                this.lineSelect = sender as childWindow.LineSelChildWin;

                if (this.lineSelect.DialogResult == true)
                {
                    //将LINE记录值添加逗号(多值时-用于界面上的TEXT中显示值)
                    if (ClientDataList.SelLineList.Count > 0)
                    {
                        #region 多值时,添加逗号
                        StringBuilder sb = new StringBuilder();
                        foreach (string str in ClientDataList.SelLineList)
                        {
                            sb.Append(str + ",");
                        }

                        this.tbSelContext.Text = string.Empty;

                        // 去除尾部多余逗号
                        this.tbSelContext.Text = sb.ToString().Remove(sb.ToString().Length - 1, 1);
                        #endregion 多值时,添加逗号

                        this.tbSelContext.Text = string.Empty;
                        this.tbSelContext.Text = this.lineSelect.retRadioButtonContent.ToString();
                        return;
                    }
                    //取消所有选择时或是没有选择时，清空文本
                    else
                    {
                        this.tbSelContext.Text = string.Empty;
                    }
                }

                if (this.lineSelect.DialogResult == false)
                {

                    //当按取消按钮时，清空文本 v2.14
                    this.tbSelContext.Text = string.Empty;

                    return;
                }
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr +
                                                ctrlName +
                                                " lineSelectWin_Closed 转换Line信息时出错" +
                                                ex.Message.ToString());

                this.btnSelList.IsEnabled = true;

                Common.FunctionList.FunctionClass.ShowMessageBox(
                   this.errorMessbox,
                   1,
                   "提示",
                   ex.Message.ToString(),
                   Colors.Gray,
                   true,
                   Visibility.Visible,
                   Visibility.Collapsed);
                return;
            }
        }

        /// <summary>
        /// ... 按钮，显示LINE窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnSelContext_Click(object sender, RoutedEventArgs e)
        {
            this.lineSelect.Title = "线路查询子窗体";
            this.lineSelect.Show();
        }
        #endregion 子窗体相关

        #region 日期(DatePicker)
        /// <summary>
        /// 结束日期(DatePicker)
        /// DpDateTo_SelectedDateChanged
        /// </summary>
        /// <param name="sender">sender of obejct</param>
        /// <param name="e">e type of SelectionChangedEventArgs</param>
        private void DpDateTo_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dpDateTo == null || dpDateTo.SelectedDate == null)
            {
                return;
            }
            else
            {
                this.dpDateTo.Text = this.dpDateTo.SelectedDate.Value.ToString("yyyy-MM-dd");
            }
        }

        /// <summary>
        /// 开始日期(DatePicker)
        /// 防止选错时，出现BUG
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of SelectionChangedEventArgs</param>
        private void DpDateFrom_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dpDateFrom == null || dpDateFrom.SelectedDate == null)
            {
                return;
            }
            else
            {
                this.dpDateFrom.Text = this.dpDateFrom.SelectedDate.Value.ToString("yyyy-MM-dd");
            }
        }

        #endregion 日期(DatePicker)

        #region 记录用户选择的WHERE条件
        /// <summary>
        /// 记录用户选择的WHERE条件
        /// </summary>
        /// <returns></returns>
        /// <value>10/15/2010 Add </value>
        private void writeWhereLog()
        {
            StringBuilder sbWhereIntorLogs = new StringBuilder();
            try
            {
                //日志用
                sbWhereIntorLogs.Append(" 查询条件：");
                sbWhereIntorLogs.Append(" 开始日期：");
                sbWhereIntorLogs.Append(string.IsNullOrEmpty(this.dpDateFrom.Text) == true ? " null" : this.dpDateFrom.Text);
                sbWhereIntorLogs.Append(string.IsNullOrEmpty(this.tpTimeStart.Value.ToString()) == true ? " null" : Convert.ToDateTime(tpTimeStart.Value).ToString("HH:mm"));
                sbWhereIntorLogs.Append(" 结束日期：");
                sbWhereIntorLogs.Append(string.IsNullOrEmpty(this.dpDateTo.Text) == true ? " null" : this.dpDateTo.Text);
                sbWhereIntorLogs.Append(string.IsNullOrEmpty(this.tpTimeEnd.Value.ToString()) == true ? " null" : Convert.ToDateTime(tpTimeEnd.Value).ToString("HH:mm"));
                sbWhereIntorLogs.Append(" 线路选择：");
                sbWhereIntorLogs.Append(string.IsNullOrEmpty(this.tbSelContext.Text) == true ? " null" : this.tbSelContext.Text);
                sbWhereIntorLogs.Append(" 输入参数：");
                sbWhereIntorLogs.Append(string.IsNullOrEmpty(this.tbEnterPar.Text) == true ? " null" : this.tbEnterPar.Text);
            }
            catch
            {
                WhereIntorLogs = string.Empty;
            }

            WhereIntorLogs = sbWhereIntorLogs.ToString();
        }

        #endregion 记录用户选择的WHERE条件
    }
}
